package conn;

import java.util.ArrayList;
import java.util.List;

/**
 * @author xuweiwei
 * @date 2018/12/12
 */
public class Game {

    public static void play(Layout layout) {
        int row = layout.getRow();
        int col = layout.getCol();
        int x = 0;
        int y = 0;
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < col; j++) {
                if (layout.see(i, j) == Layout.FILL) {
                    x = i;
                    y = j;
                }
            }
        }
        int[][] display = new int[row][col];
        int index = 1;
        display[x][y] = index++;
        List<String> result = play(layout, x, y);

        for (String direction : result) {
            if ("左".equals(direction)) {
                y--;
            } else if ("右".equals(direction)) {
                y++;
            } else if ("上".equals(direction)) {
                x--;
            } else {
                x++;
            }
            display[x][y] = index++;
        }
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < col; j++) {
                int curr = display[i][j];
                if (curr > 0) {
                    if (curr > 9) {
                        System.out.print(" " + curr + " ");
                    } else {
                        System.out.print("  " + curr + " ");
                    }

                } else {
                    System.out.print("  x ");
                }

            }
            System.out.println();
        }
    }

    public static String direction(int x, int y) {
        if (x == -1) {
            return "上";
        } else if (x == 1) {
            return "下";
        } else if (y == -1) {
            return "左";
        } else if (y == 1) {
            return "右";
        } else {
            throw new IllegalArgumentException("参数异常,x=" + x + ",y=" + y);
        }
    }

    public static List<String> play(Layout layout, int x, int y) {
        for (int i = -1; i <= 1; i++) {
            for (int j = -1; j <= 1; j++) {
                if (Math.abs(i) + Math.abs(j) == 1) {
                    if (layout.see(x + i, y + j) == Layout.BLANK) {
                        layout.fill(x + i, y + j);
                        if (layout.over()) {
                            List<String> result = new ArrayList<String>();
                            result.add(direction(i, j));
                            return result;
                        }
                        if (layout.countSinglePoint(x + i, y + j) > 1) {
                            //孤点超过1个,失败
                            layout.setBlank(x + i, y + j);
                        } else {
                            //可以尝试
                            List<String> r = play(layout, x + i, y + j);
                            if (r == null) {
                                layout.setBlank(x + i, y + j);
                            } else {
                                r.add(0, direction(i, j));
                                return r;
                            }
                        }
                    }
                }
            }
        }
        return null;
    }


    public static void main(String[] args) {
        byte[][] position = {
                {-1, 0, -1, 0, 0,0},
                {0,  0, 0, 0, 1,0},
                {0, 0, 0, 0, 0,0},
                {0, 0, 0, 0, 0,0},
                {-1, -1, 0, 0,0, -1}};
        Layout layout = new Layout(position);
        play(layout);
    }

}
